<?php

namespace backend\controllers;

use Yii;
use backend\models\UserDiy;
use backend\models\UserDiySearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use backend\models\UserInvite;
use backend\models\UserDiyPos;
use yii\web\ForbiddenHttpException;

/**
 * UserDiyController implements the CRUD actions for UserDiy model.
 */
class UserDiyController extends Controller
{
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }
    public function beforeAction($action)
    {
    	//如果未登录，则直接跳转
    	if(Yii::$app->user->isGuest){
    		return $this->redirect(['/site/login']);
    	}
    
    	$action = Yii::$app->controller->action->id;
    	$controller = Yii::$app->controller->id;
    	if(\Yii::$app->user->can('user-diy')){
    		return true;
    	}else{
    		throw new ForbiddenHttpException(Yii::t('app','Sorry , You don\'t have permission'));
    	}
    }
    /**
     * Lists all UserDiy models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new UserDiySearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single UserDiy model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
    	$model =  $this->findModel($id);
    	$invite_users = UserInvite::get_invite_all_users($model['user_id']);
        return $this->render('view', [
            'model' => $model,
        	'invite_users'=>$invite_users,
        ]);
    }

    /**
     * Creates a new UserDiy model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    /*
    public function actionCreate()
    {
        $model = new UserDiy();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }
*/
    /**
     * Updates an existing UserDiy model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    /*
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }
*/
    /**
     * Deletes an existing UserDiy model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    /*
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }
*/
    /**
     * Finds the UserDiy model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return UserDiy the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = UserDiy::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
   /*分享diy*/
    public function actionShareDiy($all_user,$from_id,$from_uid){
    	$item = UserDiy::findOne(['id'=>$from_id]);
    	if (is_null($item)/* || !array_key_exists('id' , $item) == false*/){
			$message = "来源不存在".$from_id.$item['id'];
			return $message ;
		}	
    	$all_user_arr = explode(',', $all_user);
    	foreach ($all_user_arr as $key => $val){
    		if(empty($val)) continue;
    		$itemto = UserDiy::findOne(['user_id'=>$val,'from_id'=>$from_id]);
    		//if(array_key_exists('id' , $itemto) == true){
    		if($itemto){
    			continue;
    		}
    		$model_add = new UserDiy();
    		$model_add->user_id=$val;
    		$model_add->diy_id=$item['diy_id'];
    		$model_add->name=$item['name'];
    		$model_add->tips=$item['tips'];
    		$model_add->img_small_id=$item['img_small_id'];
    		$model_add->img_thumb_id=$item['img_thumb_id'];
    		$model_add->img_url_id=$item['img_url_id'];
    		$model_add->create_time=$item['create_time'];
    		$model_add->from_uid=$from_uid;
    		$model_add->from_id=$from_id;
    		$model_add->save();
    		//pos表
    		$pos_list = UserDiyPos::findAll(['user_diy_id'=>$from_id]);
    		foreach ($pos_list as $key => $value){
	    		$model_diy_pos = new UserDiyPos();
	    		$model_diy_pos->user_diy_id = $model_add['id'];
	    		$model_diy_pos-> pos_id = $value['pos_id'];
	    		$model_diy_pos-> img_id = $value['img_id'];
	    		$model_diy_pos-> item_id = $value['item_id'];
	    		
	    		$model_diy_pos->save();
    		}
    	}
    	$message = "分享成功" ;
    	return $message;
    }
    
}
